<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<script type="text/javascript">
  document.observe("dom:loaded", function() {

    // File upload
<?php for($i=1; $i<=sfConfig::get('app_number_image_allowed'); $i++):?>

    var photo_button_<?php echo $i;?> = $('photo_upload_button_<?php echo $i;?>');
    var photo_image_<?php echo $i?> = $('photo_<?php echo $i;?>');
    new Ajax_upload(photo_button_<?php echo $i;?>,{
      //action: 'upload-test.php',
      action: '<?php echo url_for('vehicle/uploadPhoto')?>',
      name: 'image',
      responseType: 'json',
      onSubmit : function(file, ext){
        $('photo_error_<?php echo $i;?>').innerHTML = '';
        $('photo_error_<?php echo $i;?>').hide();
        photo_button_<?php echo $i;?>.update('<?php echo __('Uploading')?>');
        this.disable();
        photo_button_<?php echo $i;?>.addClassName('uploading');
        $('photo_preloader_<?php echo $i;?>').show();
        $('remove_link_<?php echo $i;?>').hide();
      },
      onComplete: function(file, response){
        photo_button_<?php echo $i;?>.removeClassName('uploading');
        $('photo_preloader_<?php echo $i;?>').hide();
        this.enable();
        if(!response['error']){
          photo_image_<?php echo $i;?>.src = response['src'];
          photo_button_<?php echo $i;?>.update('<?php echo __('Change picture')?>');
          $('vehicle_picture_<?php echo $i;?>').value = response['filename'];
          $('remove_link_<?php echo $i;?>').innerHTML = "<?php echo __('Remove')?>";
          $('remove_link_<?php echo $i;?>').className = "move delete";
          $('remove_link_<?php echo $i;?>').show();

          $('photo_<?php echo $i;?>').className = "thumb";
        }else{
          photo_image_<?php echo $i;?>.src = '/images/placeholder.jpg';
          $('photo_error_<?php echo $i;?>').innerHTML = response['error'];
          $('photo_error_<?php echo $i;?>').show();
          photo_button_<?php echo $i;?>.update('<?php echo __('Upload picture')?>');
        }
      }
    });

<?php endfor; ?>
  });

  function removePicture(pictureId){
    $('vehicle_picture_'+pictureId).value = "";
    $('photo_'+pictureId).className = "thumb deleted";
    $('remove_link_'+pictureId).innerHTML = "<?php echo __('Removed <br />(save to confirm)')?>";
    $('remove_link_'+pictureId).className = "move deleted";
    $('photo_upload_button_'+pictureId).update('<?php echo __('Upload picture')?>');
  }
  Event.observe(window, 'load', function() {
    $('vehicle_make_id').observe('change', function(e){
      new Ajax.Updater({
        success: 'vehicle_model_id',
        failure: 'notice' },
      '<?php echo url_for('vehicle/updatemodel')?>?id='+$('vehicle_make_id').value,
      { });
      $('vehicle_model_id').disabled=false;
    });
  });

</script>
<div class="sf_admin_form">
  <?php echo form_tag_for($form, '@vehicle') ?>
  <?php echo $form->renderHiddenFields(false) ?>

  <?php if ($form->hasGlobalErrors()): ?>
    <?php echo $form->renderGlobalErrors() ?>
  <?php endif; ?>

  <?php foreach ($configuration->getFormFields($form, $form->isNew() ? 'new' : 'edit') as $fieldset => $fields): ?>
    <?php include_partial('vehicle/form_fieldset', array('vehicle' => $vehicle, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>
  <?php endforeach; ?>



  <div id="add_picture">
    <ul id="pictures">
      <?php
      for($i=1; $i<=sfConfig::get('app_number_image_allowed'); $i++):
        ?>
      <li>
        <label><?php echo __('Picture')." ".$i;?><?php if($i==0):?><span class="mandatory">*</span><?php endif?></label>
          <?php if($vehicle->getPicture($i)):?>
        <img src="<?php echo $vehicle->getPicture($i)->getSmallImage()?>" id="photo_<?php echo $i?>" class="thumb" />
          <?php else :?>
        <img src="/images/placeholder.jpg" id="photo_<?php echo $i?>" class="thumb" />
          <?php endif;?>

        <div class="actions">
          <a id="photo_upload_button_<?php echo $i?>" class="action upload"><?php echo (!$vehicle->getPicture($i)) ? __('Upload picture') : __('Change picture'); ?></a>
          <span style="display:<?php echo ($vehicle->getPicture($i))?'block':'none'?>">&nbsp;<a id="remove_link_<?php echo $i?>" class="delete" onClick="removePicture(<?php echo $i?>);" ><?php echo __('Remove')?></a></span>
        </div>

        <img id="photo_preloader_<?php echo $i?>" class="preloader_right" src="/images/ajax-loader.gif" style="display:none" />
          <?php echo $form['picture_'.$i]->renderError();?>
        <span id="photo_error_<?php echo $i?>" class="field_error" style="display:none"></span>


      </li>
      <?php endfor;?>

    </ul>
  </div>
  <?php include_partial('vehicle/form_actions', array('vehicle' => $vehicle, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>
</form>
</div>
